package servlet;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import dto.RentDto;
import entity.Rent;
import service.RentService;
import service.impl.RentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/update")
public class Update extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");

        RentService rentService=new RentServiceImpl();
        BufferedReader reader = req.getReader();
        StringBuffer stringBuffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }
        String sb = stringBuffer.toString();
        Rent rent = JSON.parseObject(sb, Rent.class);
        rent.setStatus(1);
        int update = rentService.update(rent);
        System.out.println(rent.getId());
        PrintWriter out = resp.getWriter();
        if (update>0){
            RentDto rentDto=new RentDto("200","修改成功",rent);
            out.write(JSON.toJSONString(rentDto));
        }else {
            RentDto rentDto=new RentDto("500","修改失败",rent);
            out.write(JSON.toJSONString(rentDto));

        }
        out.flush();
        out.close();
        reader.close();

    }
}
